package com.benshou.bsoa.service;

import com.benshou.bsoa.common.dto.ResponseVo;
import com.benshou.bsoa.common.util.CglibCopy;
import com.benshou.bsoa.controller.req.TestEchoReq;
import com.benshou.bsoa.controller.rsp.TestEchoRsp;
import com.benshou.bsoa.dao.TestDao;
import com.benshou.bsoa.dao.params.TestParams;
import com.benshou.bsoa.dto.model.TestDto;
import com.benshou.bsoa.model.Test;
import com.benshou.bsoa.sao.FeikongSao;
import jakarta.annotation.Resource;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author <a href="mailto:liuzhenbang@benshou.com">Liu Zhenbang</a>
 */
@Service
@RequiredArgsConstructor
public class TestService {

    private final TestDao testDao;

    public ResponseVo<TestEchoRsp> echo(@NonNull TestEchoReq req) {
        var rsp = new TestEchoRsp();
        Optional.ofNullable(testDao.getByTestNo(req.getTestNo()))
                .map(test -> CglibCopy.copyCreating(test, TestDto::new))
                .ifPresent(rsp::setTest);

        return ResponseVo.success(rsp);
    }

    //新增
    public ResponseVo<Void> addTest(@NonNull Test test) {
        if (testDao.getByTestNo(test.getTestNo()) != null) {
            return ResponseVo.fail("testNo 已存在");
        }
        boolean success = testDao.insertTest(test);
        return success ? ResponseVo.success() : ResponseVo.fail("新增失败");
    }
}
